package com.zpark.it.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpark.it.clients.ProductClient;
import com.zpark.it.mapper.CollectMapper;
import com.zpark.it.param.ProductCollectParam;
import com.zpark.it.pojo.Collect;
import com.zpark.it.service.CollectService;
import com.zpark.it.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: CollectServiceImpl
 * @Author Charles
 * @Package com.zpark.it.service.impl
 * @Date 2023/7/25 17:06
 */
@Service
@Slf4j
public class CollectServiceImpl implements CollectService {

    @Autowired

    private CollectMapper collectMapper;

    @Autowired
    private ProductClient productClient;

    /**
     * 添加收藏
     *
     * @param collect
     * @return
     */
    @Override
    public R save(Collect collect) {
        //1.查询是否存在
        QueryWrapper<Collect> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",collect.getUserId());
        queryWrapper.eq("product_id",collect.getProductId());

        Long count = collectMapper.selectCount(queryWrapper);
        if (count>0){
            return R.fail("收藏已经添加，不需进行二次添加");
        }

        //2.不存在进行添加
        collect.setCollectTime(System.currentTimeMillis());
        int rows = collectMapper.insert(collect);
        log.info("业务结束：{}",rows);
        return R.ok("添加收藏成功！");
    }

    /**
     * 根据用户id，查询商品信息集合
     *
     * @param userId
     * @return
     */
    @Override
    public R list(Integer userId) {

        QueryWrapper<Collect> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.select("product_id");

        List<Object> idsObjects = collectMapper.selectObjs(queryWrapper);

        ProductCollectParam productCollectParam=new ProductCollectParam();

        List<Integer> ids=new ArrayList<>();
        for (Object o : idsObjects) {
            ids.add((Integer) o);
        }
        productCollectParam.setProductIds(ids);

        R r = productClient.productIds(productCollectParam);
        log.info("业务完成：{}",r);
        return r;
    }

    /**
     * 根据用户id、和商品id删除收藏数据
     *
     * @param collect 包含用户id
     * @return
     */
    @Override
    public R remove(Collect collect) {

        QueryWrapper<Collect> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",collect.getUserId());
        queryWrapper.eq("product_id",collect.getProductId());

        int rows = collectMapper.delete(queryWrapper);
        log.info("业务结束：{}",rows);
        return R.ok("收藏移除成功");
    }

    /**
     * 根据商品id删除购物车
     *
     * @param productId
     * @return
     */
    @Override
    public R removeByPid(Integer productId) {
        QueryWrapper<Collect> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_id",productId);

        int rows = collectMapper.delete(queryWrapper);

        return R.ok("收藏商品删除成功");
    }
}
